//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class AffiliateDBManager
    {
        #region Methods
        public static Affiliate GetByAffiliateID(int AffiliateID)
        {
            Affiliate affiliate = null;
            if (AffiliateID == 0)
                return affiliate;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_AffiliateLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "AffiliateID", DbType.Int32, AffiliateID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    affiliate = new Affiliate();
                    affiliate.AffiliateID = NopDataHelper.GetInt(dataReader, "AffiliateID");
                    affiliate.FirstName = NopDataHelper.GetString(dataReader, "FirstName");
                    affiliate.LastName = NopDataHelper.GetString(dataReader, "LastName");
                    affiliate.MiddleName = NopDataHelper.GetString(dataReader, "MiddleName");
                    affiliate.PhoneNumber = NopDataHelper.GetString(dataReader, "PhoneNumber");
                    affiliate.Email = NopDataHelper.GetString(dataReader, "Email");
                    affiliate.FaxNumber = NopDataHelper.GetString(dataReader, "FaxNumber");
                    affiliate.Company = NopDataHelper.GetString(dataReader, "Company");
                    affiliate.Address1 = NopDataHelper.GetString(dataReader, "Address1");
                    affiliate.Address2 = NopDataHelper.GetString(dataReader, "Address2");
                    affiliate.City = NopDataHelper.GetString(dataReader, "City");
                    affiliate.StateProvince = NopDataHelper.GetString(dataReader, "StateProvince");
                    affiliate.ZipPostalCode = NopDataHelper.GetString(dataReader, "ZipPostalCode");
                    affiliate.CountryID = NopDataHelper.GetInt(dataReader, "CountryID");
                    affiliate.Deleted = NopDataHelper.GetBoolean(dataReader, "Deleted");
                    affiliate.Active = NopDataHelper.GetBoolean(dataReader, "Active");
                }
            }
            return affiliate;
        }

        public static AffiliateCollection GetAllAffiliates()
        {

            AffiliateCollection affiliateCollection = new AffiliateCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_AffiliateLoadAll");
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    Affiliate affiliate = new Affiliate();
                    affiliate.AffiliateID = NopDataHelper.GetInt(dataReader, "AffiliateID");
                    affiliate.FirstName = NopDataHelper.GetString(dataReader, "FirstName");
                    affiliate.LastName = NopDataHelper.GetString(dataReader, "LastName");
                    affiliate.MiddleName = NopDataHelper.GetString(dataReader, "MiddleName");
                    affiliate.PhoneNumber = NopDataHelper.GetString(dataReader, "PhoneNumber");
                    affiliate.Email = NopDataHelper.GetString(dataReader, "Email");
                    affiliate.FaxNumber = NopDataHelper.GetString(dataReader, "FaxNumber");
                    affiliate.Company = NopDataHelper.GetString(dataReader, "Company");
                    affiliate.Address1 = NopDataHelper.GetString(dataReader, "Address1");
                    affiliate.Address2 = NopDataHelper.GetString(dataReader, "Address2");
                    affiliate.City = NopDataHelper.GetString(dataReader, "City");
                    affiliate.StateProvince = NopDataHelper.GetString(dataReader, "StateProvince");
                    affiliate.ZipPostalCode = NopDataHelper.GetString(dataReader, "ZipPostalCode");
                    affiliate.CountryID = NopDataHelper.GetInt(dataReader, "CountryID");
                    affiliate.Deleted = NopDataHelper.GetBoolean(dataReader, "Deleted");
                    affiliate.Active = NopDataHelper.GetBoolean(dataReader, "Active");
                    affiliateCollection.Add(affiliate);
                }
            }

            return affiliateCollection;
        }

        public static Affiliate InsertAffiliate(string FirstName, string LastName, string MiddleName,
            string PhoneNumber, string Email, string FaxNumber, string Company, string Address1,
            string Address2, string City, string StateProvince, string ZipPostalCode,
            int CountryID, bool Deleted, bool Active)
        {
            Affiliate affiliate = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_AffiliateInsert");
            db.AddOutParameter(dbCommand, "AffiliateID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "FirstName", DbType.String, FirstName);
            db.AddInParameter(dbCommand, "LastName", DbType.String, LastName);
            db.AddInParameter(dbCommand, "MiddleName", DbType.String, MiddleName);
            db.AddInParameter(dbCommand, "PhoneNumber", DbType.String, PhoneNumber);
            db.AddInParameter(dbCommand, "Email", DbType.String, Email);
            db.AddInParameter(dbCommand, "FaxNumber", DbType.String, FaxNumber);
            db.AddInParameter(dbCommand, "Company", DbType.String, Company);
            db.AddInParameter(dbCommand, "Address1", DbType.String, Address1);
            db.AddInParameter(dbCommand, "Address2", DbType.String, Address2);
            db.AddInParameter(dbCommand, "City", DbType.String, City);
            db.AddInParameter(dbCommand, "StateProvince", DbType.String, StateProvince);
            db.AddInParameter(dbCommand, "ZipPostalCode", DbType.String, ZipPostalCode);
            db.AddInParameter(dbCommand, "CountryID", DbType.Int32, CountryID);
            db.AddInParameter(dbCommand, "Deleted", DbType.Boolean, Deleted);
            db.AddInParameter(dbCommand, "Active", DbType.Boolean, Active);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int AffiliateID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@AffiliateID"));
                affiliate = GetByAffiliateID(AffiliateID);
            }
            return affiliate;
        }

        public static Affiliate UpdateAffiliate(int AffiliateID, string FirstName, string LastName,
            string MiddleName, string PhoneNumber, string Email, string FaxNumber, string Company,
            string Address1, string Address2, string City, string StateProvince,
            string ZipPostalCode, int CountryID, bool Deleted, bool Active)
        {
            Affiliate affiliate = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_AffiliateUpdate");
            db.AddInParameter(dbCommand, "AffiliateID", DbType.Int32, AffiliateID);
            db.AddInParameter(dbCommand, "FirstName", DbType.String, FirstName);
            db.AddInParameter(dbCommand, "LastName", DbType.String, LastName);
            db.AddInParameter(dbCommand, "MiddleName", DbType.String, MiddleName);
            db.AddInParameter(dbCommand, "PhoneNumber", DbType.String, PhoneNumber);
            db.AddInParameter(dbCommand, "Email", DbType.String, Email);
            db.AddInParameter(dbCommand, "FaxNumber", DbType.String, FaxNumber);
            db.AddInParameter(dbCommand, "Company", DbType.String, Company);
            db.AddInParameter(dbCommand, "Address1", DbType.String, Address1);
            db.AddInParameter(dbCommand, "Address2", DbType.String, Address2);
            db.AddInParameter(dbCommand, "City", DbType.String, City);
            db.AddInParameter(dbCommand, "StateProvince", DbType.String, StateProvince);
            db.AddInParameter(dbCommand, "ZipPostalCode", DbType.String, ZipPostalCode);
            db.AddInParameter(dbCommand, "CountryID", DbType.Int32, CountryID);
            db.AddInParameter(dbCommand, "Deleted", DbType.Boolean, Deleted);
            db.AddInParameter(dbCommand, "Active", DbType.Boolean, Active);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                affiliate = GetByAffiliateID(AffiliateID);

            return affiliate;
        }

        #endregion
    }
}
